#include "BoardCommunicationModule.h"
#include "I2CInitHelper.h"
#include "SimpleCompletion.h"

#include "BufDef.h"

#include <string.h>

// 无Timer0的串口接收
rt_sem_t usart_transport_check_sem;



uint32_t u9_standy_by = 0;
void Check_Usart_Receive_Data_Task(void *parmameter) {
  for (;;) {
    completion_wait(usart_transport_check_sem, RT_WAITING_FOREVER);
    {
      // 串口9接收处理
      if (u9_rx_size != 0) {
        if (u9_standy_by != 0 && u9_standy_by == u9_rx_size) {
          // 接收完成
          //				rt_kprintf("get data: %d\nget str: %s\n", u9_rx_size, usart_9_rx_msq_buf);
          //				rt_thread_mdelay(1);
          usart_9_rx_msq_buf[3] = u9_rx_size % 0x100;
          usart_9_rx_msq_buf[2] = (u9_rx_size >> 8) % 0x100;
          usart_9_rx_msq_buf[1] = (u9_rx_size >> 16) % 0x100;
          usart_9_rx_msq_buf[0] = (u9_rx_size >> 24) % 0x100;
          rt_mq_send_wait(&rs485_3_usart_rx_mq, (void *) usart_9_rx_msq_buf, 1024, 500);

          // 重置接收通道
          //completion_done(rs485_3_usart_rx_sem);
          memset((void *) usart_9_rx_msq_buf, 0, u9_rx_size + 4);
          u9_standy_by = 0;
          u9_rx_size = 0;
          //DMA_ReconfigCmd(CM_DMA2, DMA_CH1);
          AOS_SW_Trigger();
        } else {
          u9_standy_by = u9_rx_size;
        }
      }
      //GPIO_TogglePins(LED_RUN_PORT, LED_RUN_PIN);
    }
    // 尝试让渡CPU执行
    SWDT_FeedDog();
    rt_thread_yield();
  }
}
